/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainAdminForm.java
 *
 * Created on Apr 5, 2012, 9:31:15 AM
 */
package View;

import Admin.*;
import Constructor.Common;
import Util.DataUtil;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.sql.Connection;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame.JDesktopIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Admin
 */
public final class MainUserForm extends javax.swing.JFrame {

    JDesktopPane desktopPane;
    Container container = null;
    JPanel panelBgImg;
    Connection conn = null;
    DataUtil util = new DataUtil();

    /** Creates new form MainAdminForm */
    public MainUserForm() {
        LoadForm();
        initComponents();
        backgroundImage();
        conectSQL();
    }

    public void conectSQL() {
        conn = util.getconnect();
        if (conn != null) {
            this.butPlayGame.setVisible(false);
            this.butLogin.setVisible(true);
            this.butExit.setVisible(true);
            this.butCreateAccount.setVisible(true);
            this.butViewRules.setVisible(true);
            butChangPassword.setVisible(false);

        } else {
            this.butPlayGame.setVisible(false);
            this.butLogin.setVisible(true);
            this.butExit.setVisible(true);
            this.butCreateAccount.setVisible(true);
            this.butViewRules.setVisible(true);
            butChangPassword.setVisible(false);
            lbltilte.setText("Connect Database faile !!");
        }

    }

    public final void LoadForm() {
        this.setTitle("Quiz Game Show Manager");
        desktopPane = new JDesktopPane();
        this.setContentPane(desktopPane);
        ImageIcon icon = new ImageIcon(getClass().getResource("/Images/logo.png"));
        Image image = icon.getImage();
        setIconImage(image);
        int width = 1000;
        int height = 650;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);
        setResizable(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }

    public final void backgroundImage() {
        container = getContentPane();
        container.setLayout(null);
        ImageIcon img = new ImageIcon(this.getClass().getResource("/Images/background.jpg"));
        panelBgImg = new JPanel() {

            @Override
            public void paintComponent(Graphics g) {
                Image img1 = new ImageIcon(this.getClass().getResource("/Images/background.jpg")).getImage();
                Dimension size = new Dimension(img1.getWidth(null), img1.getHeight(null));
                setPreferredSize(size);
                setMaximumSize(size);
                setMinimumSize(size);
                setSize(size);
                setLayout(null);
                g.drawImage(img1, 0, 0, null);
            }
        };
        container.add(panelBgImg);
        panelBgImg.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());

        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        butChangPassword = new javax.swing.JButton();
        butPlayGame = new javax.swing.JButton();
        butCreateAccount = new javax.swing.JButton();
        butViewRules = new javax.swing.JButton();
        butLogin = new javax.swing.JButton();
        butExit = new javax.swing.JButton();
        lbltilte = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        butChangPassword.setText("Change Password");
        butChangPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butChangPasswordActionPerformed(evt);
            }
        });

        butPlayGame.setText("Play Game");
        butPlayGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butPlayGameActionPerformed(evt);
            }
        });

        butCreateAccount.setText("Create Account");
        butCreateAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butCreateAccountActionPerformed(evt);
            }
        });

        butViewRules.setText("View Rules");

        butLogin.setText("Login");
        butLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butLoginActionPerformed(evt);
            }
        });

        butExit.setText("Exit");
        butExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butExitActionPerformed(evt);
            }
        });

        lbltilte.setText("WellCome");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbltilte, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(329, 329, 329)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(butCreateAccount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(butViewRules, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(butPlayGame, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(butChangPassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(butExit, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(butLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(342, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lbltilte)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 534, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(butPlayGame)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(butViewRules)
                        .addGap(11, 11, 11)
                        .addComponent(butCreateAccount))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(butChangPassword)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(butLogin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(butExit)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void butChangPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butChangPasswordActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_butChangPasswordActionPerformed

    private void butPlayGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butPlayGameActionPerformed
        // TODO add your handling code here:
        PlayGameForm frm = new PlayGameForm();
        frm.setVisible(true);
        MainUserForm mainfrm = new MainUserForm();
        mainfrm.setVisible(false);
}//GEN-LAST:event_butPlayGameActionPerformed

    private void butCreateAccountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butCreateAccountActionPerformed
        // TODO add your handling code here:
        CreateAccountForm frm = new CreateAccountForm();
        this.dispose();
        frm.setVisible(true);
}//GEN-LAST:event_butCreateAccountActionPerformed

    private void butLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butLoginActionPerformed
        // TODO add your handling code here:
        if (butLogin.getText().equals("Login")) {
            this.setContentPane(desktopPane);
            LoginForm frm = new LoginForm(this, true);
            boolean rs = frm.returnGetString();
            if (rs == true) {
                if (Common._roles == 0) {
                    MainAdminForm fm = new MainAdminForm();
                    this.dispose();
                    fm.setVisible(true);
                    //lbltilte.setText("Wellcome " + Common._fullName + " (Admin)");
                    // butLogin.setText("Logout");
                    //butPlayGame.setText("Question");
                    // butPlayGame.setVisible(true);
                    //butChangPassword.setVisible(true);
                } else {
                    lbltilte.setText("Wellcome " + Common._fullName);
                    butLogin.setText("Logout");
                    butPlayGame.setText("Play Game");
                    butPlayGame.setVisible(true);
                    butChangPassword.setVisible(true);
                }
            }
        } else {
            butPlayGame.setText("Play Game");
            butPlayGame.setVisible(false);
            butLogin.setText("Login");
            Common._fullName = "";
            Common._UserID = -1;
            butChangPassword.setVisible(false);
            lbltilte.setText("wellcome the user!!!!!! ");
        }
}//GEN-LAST:event_butLoginActionPerformed

    private void butExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butExitActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, "Do you really want to exit? ") == JOptionPane.YES_OPTION) {
            System.exit(1);
        }
}//GEN-LAST:event_butExitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainUserForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butChangPassword;
    private javax.swing.JButton butCreateAccount;
    private javax.swing.JButton butExit;
    private javax.swing.JButton butLogin;
    private javax.swing.JButton butPlayGame;
    private javax.swing.JButton butViewRules;
    private javax.swing.JLabel lbltilte;
    // End of variables declaration//GEN-END:variables
}
